package main

/*
@Project        :   server_mon
@FileName       :   main.go
@Author         :   Administrator
@Datetime       :   2020/10/22 0:37
@Software       :   GoLand
@Show           :
*/

import (
	"fmt"
	"github.com/shirou/gopsutil/cpu"
	"github.com/shirou/gopsutil/load"
	"github.com/shirou/gopsutil/mem"
	"github.com/shirou/gopsutil/net"
	"time"
)
type ServerInfo struct {
	MenIo float64
	CpuIo float64
	Avg float64
	Address string
}

func main(){
	for {
		t := time.Now().Format("05")
		if t[1:] == "9"{
			serverInfo := GetServerInfo()
			fmt.Println("-----------------------------------------")
			fmt.Printf("内存使用率:%f\nCPU使用率:%f\n负载率:%f\nip地址:%s\n", serverInfo.MenIo, serverInfo.CpuIo,
				serverInfo.Avg, serverInfo.Address)
			fmt.Println(time.Now().Format("2006年01月02日 15点04分05秒"))
		}
	}
}

func GetServerInfo() ServerInfo {
	var serverInfo ServerInfo
	MemInfo, _ := mem.VirtualMemory()
	serverInfo.MenIo = MemInfo.UsedPercent // 内存使用率
	percent, _ := cpu.Percent(time.Second, false) // CPU使用率
	serverInfo.CpuIo = percent[0]
	info, _ := load.Avg() // Avg
	serverInfo.Avg = info.Load1
	addrs, _ := net.Interfaces()
	for addrss := range addrs{
		if addrs[addrss].Name == "eth0"{
			 serverInfo.Address = addrs[addrss].Addrs[0].Addr
		}
	}
	return serverInfo
}